package org.millioncall.yueyoga.admin.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.millioncall.openplatform.common.PaginateParams;
import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.openplatform.common.StartLimitPairImpl;
import org.millioncall.openplatform.dao.PreparedQuery;
import org.millioncall.yueyoga.admin.dao.BaseDao;
import org.millioncall.yueyoga.admin.dao.LiftClubDAO;
import org.millioncall.yueyoga.admin.model.AviatorExpression;
import org.millioncall.yueyoga.admin.model.HomePageLiftClub;
import org.millioncall.yueyoga.admin.model.LiftClub;
import org.millioncall.yueyoga.admin.model.LiftClubAdvance;
import org.millioncall.yueyoga.admin.model.LiftClubRecord;
import org.millioncall.yueyoga.admin.model.MemberClub;
import org.millioncall.yueyoga.admin.model.MemberLiftClub;
import org.millioncall.yueyoga.admin.service.advancedsearch.AviatorResultHandler;
import org.springframework.stereotype.Repository;

@Repository
public class LiftClubDAOImpl extends BaseDao implements LiftClubDAO {

	@Override
	public PaginatedList<LiftClub> findInPage(int start, int limit,
			String[] paramNames, String[] paramValues) {
		try {
			PreparedQuery<LiftClub> pq = this.getPagableSqlSessionTemplate()
					.findAll("findAllLiftClub");
			PaginatedList<LiftClub> pl = pq.list(new PaginateParams(
					new StartLimitPairImpl(start, limit)));
			return pl;
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public PaginatedList<LiftClub> findInPage(int start, int limit) {
		try {
			PreparedQuery<LiftClub> pq = this.getPagableSqlSessionTemplate()
					.findAll("findAllLiftClub");
			PaginatedList<LiftClub> pl = pq.list(new PaginateParams(
					new StartLimitPairImpl(start, limit)));
			return pl;
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public int deleteByIds(String[] ids) {
		int Id = 0;
		for (String strId : ids) {
			try {
				Id = Integer.parseInt(strId);
				deleteById(Id);
			} catch (NumberFormatException e) {
				return 0;
			}
		}
		return 1;
	}

	public void deleteById(Integer Id) {
		try {
			this.getPagableSqlSessionTemplate().delete("deleteByLiftClub", Id);
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public LiftClub findById(Integer id) {
		LiftClub liftClub = null;
		try {
			liftClub = this.getPagableSqlSessionTemplate().selectOne(
					"findLiftClubById", id);
		} catch (Exception e) {
			throw e;
		}
		return liftClub;
	}

	@Override
	public int update(LiftClub liftClub) {
		int i = 0;
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("enddate", liftClub.getEnddate());
			map.put("startdate", liftClub.getStartdate());
			map.put("name", liftClub.getName());
			i = this.getPagableSqlSessionTemplate().update("updateLiftClub",
					map);
		} catch (Exception e) {
			throw e;
		}
		return i;
	}

	@Override
	public int save(LiftClub liftClub) {
		int i = 0;
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("enddate", liftClub.getEnddate());
			map.put("startdate", liftClub.getStartdate());
			map.put("name", liftClub.getName());
			i = this.getPagableSqlSessionTemplate().insert("saveLiftClub", map);
		} catch (Exception e) {
			throw e;
		}
		return i;
	}

	@Override
	public PaginatedList<LiftClub> findPage(int start, int limit,
			Map<String, Object> map) {
		PreparedQuery<LiftClub> pq = this.getPagableSqlSessionTemplate()
				.findAll("findIdPage", map);
		PaginatedList<LiftClub> pl = pq.list(new PaginateParams(
				new StartLimitPairImpl(start, limit)));
		return pl;
	}

	@Override
	public int addLiftClub(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().insert("saveLiftClubs", map);
	}

	@Override
	public int updateLiftClub(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().update("editLiftClubs", map);
	}

	@Override
	public int deleteByLiftClubId(List<Integer> ids) {
		return this.getPagableSqlSessionTemplate().delete("deleteByLiftClubId",
				ids);
	}

	@Override
	public LiftClub selectById(Integer id) {
		LiftClub liftClub = this.getPagableSqlSessionTemplate().selectOne(
				"findClubById", id);
		return liftClub;
	}

	@Override
	public List<LiftClub> findLiftClubAll() {
		try {
			List<LiftClub> lift = this.getPagableSqlSessionTemplate()
					.selectList("findAllLiftClub");
			return lift;
		} catch (RuntimeException re) {
			throw re;
		}
	}

	@Override
	public List<MemberClub> findLiftClubId(Integer id) {
		List<MemberClub> lift = this.getPagableSqlSessionTemplate().selectList(
				"findLiftClubId", id);
		return lift;
	}

	@Override
	public PaginatedList<LiftClub> findLiftClubById(int start, int limit,
			Integer id) {
		try {
			PreparedQuery<LiftClub> lift = this.getPagableSqlSessionTemplate()
					.findAll("findLiftClubById", id);
			PaginatedList<LiftClub> pl = lift.list(new PaginateParams(
					new StartLimitPairImpl(start, limit)));

			return pl;
		} catch (RuntimeException re) {
			throw re;
		}
	}

	@Override
	public List<HomePageLiftClub> findLastLiftClubs(int n,String organizationId) {
		PreparedQuery<HomePageLiftClub> pq = this
				.getPagableSqlSessionTemplate().findAll("findLastLiftClubs",organizationId);
		PaginatedList<HomePageLiftClub> pl = pq.list(new PaginateParams(
				new StartLimitPairImpl(0, n)));
		return pl;
	}
	
	/**
	 * 查询导出活动列表信息
	 */
	@Override
	public PaginatedList<LiftClubRecord> findLiftClubsByCdts(int start,
			int limit, Map<String, Object> map) {
		PreparedQuery<LiftClubRecord> pq = this
				.getPagableSqlSessionTemplate().findAll("findLiftClubsByCdts",
						map);
		PaginatedList<LiftClubRecord> pl = pq.list(new PaginateParams(
				new StartLimitPairImpl(start, limit)));
		return pl;
	}

	@Override
	public boolean addMemberToLiftClub(Map<String, Object> map) {
		int i =	this.getPagableSqlSessionTemplate().insert("saveMemberToLiftClub", map);
		if(i > 0) return true;
		else return false;
	}

	@Override
	public MemberLiftClub findLiftClubMember(MemberLiftClub mem) {
		return this.getPagableSqlSessionTemplate().selectOne("findByMemberLiftClub", mem);
	}

	@Override
	public boolean deleteMemberFromLiftClub(MemberLiftClub mem) {
		int i = this.getPagableSqlSessionTemplate().delete("deleteByMemberLiftClub", mem);
		if(i > 0) return true;
		else return false;
	}

	@Override
	public LiftClub findLiftClubByCdts(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().selectOne("findLiftClubByCdts", map);
	}

	@Override
	public List<LiftClubAdvance> getAllLiftClubsWithAviator(
			Map<String, Object> params, AviatorExpression expression,
			Map<String, Object> aviatorParams, int limit) {
		AviatorResultHandler<LiftClubAdvance> handler = new AviatorResultHandler<LiftClubAdvance>(
				expression, aviatorParams, limit);
		this.getPagableSqlSessionTemplate().select("findAdvanceInPage", params,
				new RowBounds(0, Integer.MAX_VALUE), handler);
		return handler.getResultList();
	}
}
